package com.ogawa.base.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * 应用程序异常类：用于捕获异常和提示错误信息
 *
 * @author ming
 * @version 1.0
 * @created 2014-12-26
 */

public class AppException extends Exception implements UncaughtExceptionHandler {
    private Context mContext;

    /**
     * 系统默认的UncaughtException处理类
     */
    private UncaughtExceptionHandler mDefaultHandler;

    private AppException(Context context) {
        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        this.mContext = context;
    }


    /**
     * 获取APP异常崩溃处理对象
     */
    public static AppException getAppExceptionHandler(Context context) {
        return new AppException(context);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if (!handleException(ex) && mDefaultHandler != null) {
            // 如果用户没有处理则让系统默认的异常处理器来处理
            mDefaultHandler.uncaughtException(thread, ex);
        } else {
            //退出程序
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
//            restart();
        }
    }

    /**
     * 自定义异常处理:收集错误信息&发送错误报告
     *
     * @param ex
     * @return true:处理了该异常信息;否则返回false
     */
    private boolean handleException(Throwable ex) {
        if (ex == null) {
            Log.e("AppException", "ex == null");
            return false;
        }
        if (mContext == null) {
            Log.e("AppException", "mContext == null");
            return false;
        }
        Log.e("AppException", "UnCaughtException save");
        Log.e("AppException", "UnCaughtException", ex);

        long crash_time = new Date().getTime();
        Long pre_crash_time = (Long) SpUtil.Companion.get("crash_time", 0);
        SpUtil.Companion.set("crash_time", crash_time);
        if(pre_crash_time != 0&&(crash_time-pre_crash_time)<5*1000){
            return false;
        }
        if(pre_crash_time == 0||(crash_time-pre_crash_time)>60*1000){
            // 直接回到首页
//            Intent intent = new Intent(mContext, MainActivity.class);//跳转到App最开始的页面
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
//                    | Intent.FLAG_ACTIVITY_CLEAR_TASK
//                    | Intent.FLAG_ACTIVITY_NEW_TASK);
////        mContext.startActivity(intent);
//            PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
//            AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
//            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 50, pendingIntent);
//            System.exit(2);
        }
        return true;
    }
}